package com.fan.admin.controller;

import com.fan.admin.service.CourseRelationService;
import com.fan.admin.service.CourseService;
import com.fan.admin.service.StudentService;
import com.fan.commons.pojo.ClaInfo;
import com.fan.commons.pojo.Course;
import com.fan.commons.pojo.CourseRelation;
import com.fan.commons.pojo.Student;
import com.fan.commons.util.JsonResult;
import com.fan.commons.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fan
 * @date 2021/3/9
 */
@RestController
public class CourseController {

	@Autowired
	private CourseService courseService;

	@Autowired
	private StudentService studentService;

	@Autowired
	private CourseRelationService courseRelationService;


	@PostMapping("course/add/{nature}")
	public JsonResult addCourse(Course course, @PathVariable Integer nature) {
		course.setNature(nature);
		int code = courseService.addCourse(course);
		if (code == 0) {
			return JsonResult.failure();
		}
		return JsonResult.success(course.getId());
	}

	@GetMapping("/course/{nature}/list")
	public JsonResult queryCourse(@PathVariable Integer nature, @Param(value = "1") Integer page, @Param(value = "5") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Course> courses = courseService.queryCourseByNature(nature);
		PageInfo<Course> pageInfo = new PageInfo<>(courses);
		PageUtil pageUtil = new PageUtil(page, pageSize, courses, pageInfo.getTotal());
		return JsonResult.success(pageUtil);
	}


	@GetMapping("/course/query/{id}")
	public JsonResult queryCourseById(@PathVariable Integer id) {
		Course course = courseService.queryCourseById(id);
		List<Course> courses = new ArrayList<>(1);
		courses.add(course);
		PageUtil pageUtil = new PageUtil(1, 1, courses, 1L);
		return JsonResult.success(pageUtil);
	}

	@PutMapping("/course/{nature}/update")
	public JsonResult updateCourse(@PathVariable Integer nature, Course course) {
		course.setNature(nature);
		int code = courseService.updateCourse(course);
		if (code == 0) {
			return JsonResult.failure();
		}
		return JsonResult.success(null);
	}

	@GetMapping("/course/delete/{id}")
	public JsonResult deleteCourse(@PathVariable Integer id) {
		Course course = courseService.queryCourseById(id);
		int code = courseService.deleteCourseById(id);
		int codeT = courseRelationService.deleteCourseByCourseId(course.getCourseId());
		if (code + codeT >= 1) {
			return JsonResult.success(null);
		}
		return JsonResult.failure();

	}

	@GetMapping("/course/student/list")
	public JsonResult courseStudent(String courseId, @Param(value = "1") Integer page, @Param(value = "5") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<CourseRelation> courseRelations = courseRelationService.queryRelation(courseId);
		List<Student> students = new ArrayList<>();
		for (CourseRelation courseRelation : courseRelations) {
			Student student = studentService.queryStudentByStudentId(courseRelation.getRelationId());
			students.add(student);
		}
		PageInfo<Student> pageInfo = new PageInfo<>(students);
		PageUtil pageUtil = new PageUtil(page, pageSize, students, pageInfo.getTotal());
		return JsonResult.success(pageUtil);
	}

	@GetMapping("/course/cla/has")
	public JsonResult queryCourseCla(String courseId, @Param(value = "1") Integer page, @Param(value = "5") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<CourseRelation> courseRelations = courseRelationService.queryRelation(courseId);
		PageInfo<CourseRelation> pageInfo = new PageInfo<>(courseRelations);
		PageUtil pageUtil = new PageUtil(page, pageSize, courseRelations, pageInfo.getTotal());
		return JsonResult.success(pageUtil);
	}

	@GetMapping("/course/cla/no")
	public JsonResult queryCourseClaNo(String courseId, @Param(value = "1") Integer page, @Param(value = "5") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<ClaInfo> claInfos = courseRelationService.queryAllNoCla(courseId);
		PageInfo<ClaInfo> pageInfo = new PageInfo<>(claInfos);
		PageUtil pageUtil = new PageUtil(page, pageSize, claInfos, pageInfo.getTotal());
		return JsonResult.success(pageUtil);
	}

	@PostMapping("/course/cla/add/")
	public JsonResult courseClaAdd(CourseRelation courseRelation) {
		Course course = courseService.queryCourseByCourseId(courseRelation.getCourseId());
		if (course == null) {
			return JsonResult.failure();
		}
		courseRelation.setNature(0);
		courseRelation.setVersion(String.valueOf(System.currentTimeMillis()));
		int coed = courseRelationService.insertCourseRelation(courseRelation);
		if (coed != 1) {
			return JsonResult.failure();
		}
		return JsonResult.success(null);
	}

	@GetMapping("/course/cla/delete/{id}")
	public JsonResult courseClaDelete(@PathVariable Integer id) {
		int code = courseRelationService.deleteRelationById(id);
		if (code == 0) {
			return JsonResult.failure();
		}
		return JsonResult.success(null);
	}
}
